package com.lvmama.pangolin.analysis;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.lvmama.pangolin.busi.config.ConfigManager;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.lmm.LMMTicketProduct;
import com.lvmama.pangolin.entity.qnr.QNRTicketProduct;
import com.lvmama.pangolin.entity.tc.TCTicketProduct;
import com.lvmama.pangolin.entity.tn.TNTicketProduct;
import com.lvmama.pangolin.entity.xc.XCTicketProduct;

@Component
public class ProductParamDataAnalysis {
	/**
	 * @author zhoutianqi
	 * @className ProductParamDataAnalysis.java
	 * @date 2017年6月16日 上午10:29:28
	 * @description
	 */

	public Map<String, String> getProductGrade(LMMTicketProduct lvItem, QNRTicketProduct qnrProductItem,
			TCTicketProduct tcProductItem, TNTicketProduct tnProductItem, XCTicketProduct xcProductItem,
			ConfigManager configManager) {
		if (qnrProductItem == null) {
			qnrProductItem = new QNRTicketProduct();
		}
		if (tcProductItem == null) {
			tcProductItem = new TCTicketProduct();
		}
		if (tnProductItem == null) {
			tnProductItem = new TNTicketProduct();
		}
		if (xcProductItem == null) {
			xcProductItem = new XCTicketProduct();
		}
		if (lvItem.getCommodityNum()==null) {
			lvItem.setCommodityNum(new Integer(0));
		}
		if (lvItem.getCommPackageNum()==null) {
			lvItem.setCommPackageNum(new Integer(0));
		}
		if (lvItem.getCommPreferentialNum()==null) {
			lvItem.setCommPreferentialNum(new Integer(0));
		}
		if (lvItem.getCommTypeNum()==null) {
			lvItem.setCommTypeNum(new Integer(0));
		}
		if (!StringTools.isValid(lvItem.getImageNum())) {
			lvItem.setImageNum("0");
		}
		if (!StringTools.isValid(qnrProductItem.getImageNum())) {
			qnrProductItem.setImageNum("0");
		}
		if (!StringTools.isValid(tcProductItem.getImageNum())) {
			tcProductItem.setImageNum("0");
		}
		if (!StringTools.isValid(tnProductItem.getImageNum())) {
			tnProductItem.setImageNum("0");
		}
		if (!StringTools.isValid(xcProductItem.getImageNum())) {
			xcProductItem.setImageNum("0");
		}
		Map<String, String> gradeMap = new HashMap<>();
		// 图片评分
		double[] picGrade = this.getPicGrade(lvItem, qnrProductItem, tcProductItem, tnProductItem, xcProductItem,
				configManager);
		// 商品数量评分
		double[] commGrade = this.getCommGrade(lvItem, qnrProductItem, tcProductItem, tnProductItem, xcProductItem,
				configManager);
		// 商品类别评分
		double[] commTypeGrade = this.getCommTypeGrade(lvItem, qnrProductItem, tcProductItem, tnProductItem,
				xcProductItem, configManager);
		// 商品优惠率评分
		double[] preGrade = this.getCommPreferentialGrade(lvItem, qnrProductItem, tcProductItem, tnProductItem,
				xcProductItem, configManager);
		// 商品打包率
		double[] packageGrade = this.getCommPackageGrade(lvItem, qnrProductItem, tcProductItem, tnProductItem,
				xcProductItem, configManager);
		gradeMap.put("lmmGrade",
				StringTools.formatDouble((picGrade[0] + commGrade[0] + commTypeGrade[0] + preGrade[0] + packageGrade[0]) * 10));
		gradeMap.put("qnrGrade",
				StringTools.formatDouble((picGrade[1] + commGrade[1] + commTypeGrade[1] + preGrade[1] + packageGrade[1]) * 10));
		gradeMap.put("tcGrade",
				StringTools.formatDouble((picGrade[2] + commGrade[2] + commTypeGrade[2] + preGrade[2] + packageGrade[2]) * 10));
		gradeMap.put("tnGrade",
				StringTools.formatDouble((picGrade[3] + commGrade[3] + commTypeGrade[3] + preGrade[3] + packageGrade[3]) * 10));
		gradeMap.put("xcGrade",
				StringTools.formatDouble((picGrade[4] + commGrade[4] + commTypeGrade[4] + preGrade[4] + packageGrade[4]) * 10));
		return gradeMap;
	}

	public double[] getPicGrade(LMMTicketProduct lvItem, QNRTicketProduct qnrProductItem, TCTicketProduct tcProductItem,
			TNTicketProduct tnProductItem, XCTicketProduct xcProductItem, ConfigManager configManager) {
		double[] result = { 0.0, 0.0, 0.0, 0.0, 0.0 };
		// 获取图片的权重
		double picWeight = Double.parseDouble(
				configManager.getParamValue("Ticket_Product_Param_Handel", "Ticket_Product_ProductPicNum_Weight"));
		// 产品图片数量
		int maxIntPic = StringTools.getMaxValue(Integer.parseInt(qnrProductItem.getImageNum()),
				Integer.parseInt(tcProductItem.getImageNum()), Integer.parseInt(tnProductItem.getImageNum()),
				Integer.parseInt(xcProductItem.getImageNum()), Integer.parseInt(lvItem.getImageNum()));
		if (maxIntPic == 0) {
			return result;
		}
		double maxPicNum = Double.parseDouble(String.valueOf(maxIntPic));
		result[0] = (Double.parseDouble(lvItem.getImageNum()) / maxPicNum) * picWeight;
		result[1] = (Double.parseDouble(qnrProductItem.getImageNum()) / maxPicNum) * picWeight;
		result[2] = (Double.parseDouble(tcProductItem.getImageNum()) / maxPicNum) * picWeight;
		result[3] = (Double.parseDouble(tnProductItem.getImageNum()) / maxPicNum) * picWeight;
		result[4] = (Double.parseDouble(xcProductItem.getImageNum()) / maxPicNum) * picWeight;
		return result;
	}

	public double[] getCommGrade(LMMTicketProduct lvItem, QNRTicketProduct qnrProductItem,
			TCTicketProduct tcProductItem, TNTicketProduct tnProductItem, XCTicketProduct xcProductItem,
			ConfigManager configManager) {
		double[] result = { 0.0, 0.0, 0.0, 0.0, 0.0 };
		double commWeight = Double.parseDouble(
				configManager.getParamValue("Ticket_Product_Param_Handel", "Ticket_Product_CommNum_Weight"));
		// 商品数量最大值
		int intCommNum = StringTools.getMaxValue(Integer.parseInt(qnrProductItem.getCommodityNum()),
				Integer.parseInt(tcProductItem.getCommodityNum()), Integer.parseInt(tnProductItem.getCommodityNum()),
				Integer.parseInt(xcProductItem.getCommodityNum()), lvItem.getCommodityNum().intValue());
		if (intCommNum == 0) {
			return result;
		}
		double maxcommNum = Double.parseDouble(String.valueOf(intCommNum));
		result[0] = (Double.parseDouble(lvItem.getCommodityNum().toString()) / maxcommNum) * commWeight;
		result[1] = (Double.parseDouble(qnrProductItem.getCommodityNum()) / maxcommNum) * commWeight;
		result[2] = (Double.parseDouble(tcProductItem.getCommodityNum()) / maxcommNum) * commWeight;
		result[3] = (Double.parseDouble(tnProductItem.getCommodityNum()) / maxcommNum) * commWeight;
		result[4] = (Double.parseDouble(xcProductItem.getCommodityNum()) / maxcommNum) * commWeight;
		return result;
	}

	public double[] getCommTypeGrade(LMMTicketProduct lvItem, QNRTicketProduct qnrProductItem,
			TCTicketProduct tcProductItem, TNTicketProduct tnProductItem, XCTicketProduct xcProductItem,
			ConfigManager configManager) {
		double[] result = { 0.0, 0.0, 0.0, 0.0, 0.0 };
		double typeWeight = Double.parseDouble(
				configManager.getParamValue("Ticket_Product_Param_Handel", "Ticket_Product_CommTypeNum_Weight"));
		// 商品类型最大值
		int intCommTypeNum = StringTools.getMaxValue(Integer.parseInt(qnrProductItem.getCommTypeNum()),
				Integer.parseInt(tcProductItem.getCommTypeNum()), Integer.parseInt(tnProductItem.getCommTypeNum()),
				Integer.parseInt(xcProductItem.getCommTypeNum()), lvItem.getCommTypeNum().intValue());
		if (intCommTypeNum == 0) {
			return result;
		}
		double maxcommTypeNum = Double.parseDouble(String.valueOf(intCommTypeNum));
		result[0] = (Double.parseDouble(lvItem.getCommTypeNum().toString()) / maxcommTypeNum) * typeWeight;
		result[1] = (Double.parseDouble(qnrProductItem.getCommTypeNum()) / maxcommTypeNum) * typeWeight;
		result[2] = (Double.parseDouble(tcProductItem.getCommTypeNum()) / maxcommTypeNum) * typeWeight;
		result[3] = (Double.parseDouble(tnProductItem.getCommTypeNum()) / maxcommTypeNum) * typeWeight;
		result[4] = (Double.parseDouble(xcProductItem.getCommTypeNum()) / maxcommTypeNum) * typeWeight;
		return result;
	}

	public double[] getCommPreferentialGrade(LMMTicketProduct lvItem, QNRTicketProduct qnrProductItem,
			TCTicketProduct tcProductItem, TNTicketProduct tnProductItem, XCTicketProduct xcProductItem,
			ConfigManager configManager) {
		double[] result = { 0.0, 0.0, 0.0, 0.0, 0.0 };
		double preWeight = Double.parseDouble(configManager.getParamValue("Ticket_Product_Param_Handel",
				"Ticket_Product_CommPreferentialNum_Weight"));
		// 商品类型最大值
		// 商品优惠最大值
		int intCommPreNum = StringTools.getMaxValue(Integer.parseInt(qnrProductItem.getCommPreferentialNum()),
				Integer.parseInt(tcProductItem.getCommPreferentialNum()),
				Integer.parseInt(tnProductItem.getCommPreferentialNum()),
				Integer.parseInt(xcProductItem.getCommPreferentialNum()), lvItem.getCommPreferentialNum().intValue());
		if (intCommPreNum == 0) {
			return result;
		}
		double maxcommPreNum = Double.parseDouble(String.valueOf(intCommPreNum));
		result[0] = (Double.parseDouble(lvItem.getCommPreferentialNum().toString()) / maxcommPreNum) * preWeight;
		result[1] = (Double.parseDouble(qnrProductItem.getCommPreferentialNum()) / maxcommPreNum) * preWeight;
		result[2] = (Double.parseDouble(tcProductItem.getCommPreferentialNum()) / maxcommPreNum) * preWeight;
		result[3] = (Double.parseDouble(tnProductItem.getCommPreferentialNum()) / maxcommPreNum) * preWeight;
		result[4] = (Double.parseDouble(xcProductItem.getCommPreferentialNum()) / maxcommPreNum) * preWeight;
		return result;
	}

	public double[] getCommPackageGrade(LMMTicketProduct lvItem, QNRTicketProduct qnrProductItem,
			TCTicketProduct tcProductItem, TNTicketProduct tnProductItem, XCTicketProduct xcProductItem,
			ConfigManager configManager) {
		double[] result = { 0.0, 0.0, 0.0, 0.0, 0.0 };
		double packageWeight = Double.parseDouble(
				configManager.getParamValue("Ticket_Product_Param_Handel", "Ticket_Product_CommPackageNum_Weight"));
		// 商品打包数量
		double intCommPackageNum = StringTools.getMaxValue(Integer.parseInt(qnrProductItem.getCommPackageNum()),
				Integer.parseInt(tcProductItem.getCommPackageNum()),
				Integer.parseInt(tnProductItem.getCommPackageNum()),
				Integer.parseInt(xcProductItem.getCommPackageNum()), lvItem.getCommPackageNum().intValue());
		if (intCommPackageNum == 0) {
			return result;
		}
		double maxcommPackageNum = Double.parseDouble(String.valueOf(intCommPackageNum));
		result[0] = (Double.parseDouble(lvItem.getCommPackageNum().toString()) / maxcommPackageNum) * packageWeight;
		result[1] = (Double.parseDouble(qnrProductItem.getCommPackageNum()) / maxcommPackageNum) * packageWeight;
		result[2] = (Double.parseDouble(tcProductItem.getCommPackageNum()) / maxcommPackageNum) * packageWeight;
		result[3] = (Double.parseDouble(tnProductItem.getCommPackageNum()) / maxcommPackageNum) * packageWeight;
		result[4] = (Double.parseDouble(xcProductItem.getCommPackageNum()) / maxcommPackageNum) * packageWeight;
		return result;
	}
}
